<?php

/**
 * 文档信息仓库
 * @author wangwx 2015-5-12
 */

namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\repositories\common\MyscrmContractSyncDeleteDataRepository;
use common\support\StringHelper;
use common\entities\myhtsq\DocumentEntity;
use common\error\ErrorCode;
use common\models\myhtsq\DocumentModel;

class DocumentRepository extends BaseRepository {

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = []) {
        parent::__construct($config);
    }

    /**
     * 文档列表
     * @param string $businessId 业务id
     * @param bool $excludePDF 是否排除PDF格式的文档
     * @return array
     */
    public static function getDocumentListByBusinessId($businessId, $excludePDF = false) {
        if($excludePDF){
            return DocumentEntity::find()
                ->where(['business_id' => $businessId])
                ->andWhere(['not',['document_ext_name' => 'pdf']])
                ->andWhere(['not',['document_ext_name' => 'PDF']])
                ->asArray()
                ->all();
        }else{
            return DocumentEntity::find()
                ->where(['business_id' => $businessId])
                ->asArray()
                ->all();
        }
    }

    /**
     * 新增文档
     * @param \common\models\myhtsq\DocumentModel $documentModel
     * @return boolean|string 失败返回false，成功返回新增的文档Id
     */
    public static function addDocument($documentModel) {
        $entity = new DocumentEntity();
        $entity->setAttributes($documentModel->attributes);
        $entity->document_id = \common\support\StringHelper::uuid();
        if($entity->save()) {
            return $entity->primaryKey;
        }else{
            return false;
        }
    }
    
    /**
     * 更新文档
     * @param \common\models\myhtsq\DocumentModel $documentModel
     * @return string|boolean
     */
    public static function updateDocument($documentModel) {
        $entity = new DocumentEntity();
        $modelInfo = $entity::find()->where(['document_id' => $documentModel->document_id])->one();
        $data = $documentModel->attributes;
        $modelInfo->load($data, '');
        if ($modelInfo->save()) {
            return $modelInfo->document_id;
        } else {
            return false;
        }
    }

    /**
     * 根据文档名称获取文档ID
     *
     * @param  $contractId  合同ID
     * @param  $name        文档名称
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getDocumentId($contractId, $name){
        $entity = new DocumentEntity();
        $result = $entity::find()->select('document_id')->where(['business_id' => $contractId, 'document_name' => $name])->scalar();

        return $result;
    }

    /**
     * 根据文件id,删除文件记录
     * @param string $documentId 文件id
     * @return boolean
     */
    public static function delDocumentById($documentId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteResult = DocumentEntity::deleteAll(['document_id' => $documentId]);

        if ($deleteResult > 0) {
            $contractSyncDeleteDataRepository->add('myscrm_document', 'document_id', $documentId);
        }

        return $deleteResult;
    }

    /**
     * 根据业务id删除文件记录
     * @param string $businessId 业务id
     * @return boolean
     */
    public static function delDocumentByBusinessId($businessId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteData = DocumentEntity::findAll(["business_id" => $businessId]);

        $deleteResult = DocumentEntity::deleteAll(["business_id" => $businessId]);

        if ($deleteResult > 0) {
            foreach ($deleteData as $data) {
                $contractSyncDeleteDataRepository->add('myscrm_document', 'document_id', $data['document_id']);
            }
        }

        return $deleteResult;
    }


    /**
     * 
     * @param $document
     * @param $platform
     * @return bool|mixed
     */
    public static function addDocumentForm($document,$platform = 1){
        if($platform == 1){
            $entity = new DocumentEntity();
            $modelInfo = $entity::find()->where(['document_id' => $document['document_id']])->one();
            if($modelInfo){
                $modelInfo->setAttributes($document);
                if ($modelInfo->save()) {
                    return $modelInfo->document_id;
                } else {
                    return false;
                }
            }else{
                $entity->setAttributes($document);
                if($entity->save()) {
                    return $entity->primaryKey;
                }else{
                    return false;
                }
            }

        }
    }

}
